package com.zzl.finance.order.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.order.constant.InvoiceCategoryEnum;
import com.zzl.finance.order.constant.InvoiceStatusEnum;
import com.zzl.finance.order.constant.InvoiceTypeEnum;
import com.zzl.finance.order.constant.OrderStatusEnum;
import com.zzl.finance.order.dto.*;
import com.zzl.finance.order.entity.OrderEntity;
import com.zzl.finance.order.service.OrderService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author sunpp
 * @date 2021-11-26 19:40:12
 */
@ApidocController("订单接口")
@RestController
@RequestMapping("order/order")
@Slf4j
public class OrderController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private OrderService orderService;

    @GetMapping("/getOrderStatusList")
    @ApidocOperation(description = "订单状态枚举值", operationId = "getOrderStatusList")
    public Result getOrderStatusList() {
        if (this.getUser().getUserCategory() == 1) {
            return Result.ok().list(OrderStatusEnum.getCoreList());
        } else {
            return Result.ok().list(OrderStatusEnum.getList());
        }
    }

    @GetMapping("/getInvoiceStatusList")
    @ApidocOperation(description = "发票状态枚举值", operationId = "getInvoiceStatusList")
    public Result getInvoiceStatusList() {
        return Result.ok().list(InvoiceStatusEnum.getList());
    }

    @GetMapping("/getInvoiceCategoryList")
    @ApidocOperation(description = "发票种类枚举值", operationId = "getInvoiceCategoryList")
    public Result getInvoiceCategoryList() {
        return Result.ok().list(InvoiceCategoryEnum.getList());
    }

    @GetMapping("/getInvoiceTypeList")
    @ApidocOperation(description = "发票类型枚举值", operationId = "getInvoiceTypeList")
    public Result getInvoiceTypeList() {
        return Result.ok().list(InvoiceTypeEnum.getList());
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询数据", operationId = "oo1")
    @ApidocReturnPage(genericTypeClass = OrderPageDto.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {
            "orderNo",
            "ticketNo",
            "paymentEnterpriseId",
            "createStartTime",
            "createEndTime",
            "orderStatus",
            "orderAmountStart",
            "orderAmountEnd"}
            , keyDescription = {"订单编号",
            "关联凭证编号",
            "付款企业id",
            "开始时间",
            "结束time",
            "处理状态 1：已支付、2：未支付",
            "订单金额开始",
            "订单金额结束"})
                       @RequestParam Map<String, Object> params) {
        params.put("enterpriseId", this.getUser().getEnterpriseId());
        return Result.ok().put("page", orderService.queryOrderPage(params, this.getUser()));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "通过id获取数据", operationId = "oo2")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = OrderDetailDto.class)
    })
    public Result info(@ApidocParam(name = "id", required = true) @PathVariable("id") String id) {
        try {
            OrderDetailDto orderDetail = orderService.getOrderDetail(id);
            return Result.ok().put("data", orderDetail);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }


    @GetMapping("/platList")
    @ApidocOperation(description = "平台--订单列表", operationId = "platList")
    @ApidocReturnPage(genericTypeClass = OrderEntity.class)
    public Result platList(@ApidocParamsPage(name = "params", keyName = {"orderNo"
            , "ticketNo", "instinctyId", "createStartTime", "createEndTime", "orderStatus", "invoiceStatus", "financeEnterpriseName"}
            , keyDescription = {"订单", "凭证", "金融机构", "开始时间", "结束time", "订单status", "发票status", "融资企业"})
                           @RequestParam Map<String, Object> params) {
        params.put("plat", "plat");
        return Result.ok().put("page", orderService.queryOrderPage(params, this.getUser()));
    }


    @GetMapping("/platInfo/{id}")
    @ApidocOperation(description = "平台--订单详情", operationId = "platInfo")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = PlatOrderDetail.class)
    })
    public Result platInfo(@ApidocParam(name = "id", required = true) @PathVariable("id") Integer id) {
        try {
            PlatOrderDetail orderDetail = orderService.getPlatOrderDetail(id);
            return Result.ok().put("data", orderDetail);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @PostMapping("/invoiceCheckSubmit")
    @ApidocOperation(description = "平台--审批提交", operationId = "invoiceCheckSubmit")
    @ApidocReturnR
    public Result invoiceCheckSubmit(@ApidocParam(name = "checkSubmit", required = true) @Validated @RequestBody CheckSubmit checkSubmit) {
        try {
            orderService.invoiceCheckSubmit(checkSubmit, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }


    @PutMapping("/updatePlatOrder")
    @ApidocOperation(description = "平台--订单详情修改", operationId = "updatePlatOrder")
    @ApidocReturnR
    public Result invoiceCheckSubmit(@ApidocParam(name = "需改参数body", required = true) @RequestBody OrderInvoiceUpdateDto dto) {
        try {
            orderService.updatePlatOrder(dto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }


}
